package cn.com.gas.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasOfflineCamEntity;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.service.GasOfflineCamService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;

/**
 * 离线摄像头监控
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-04-17 09:48:42
 */
@Controller
@RequestMapping("/gas/offlinecam/")
public class GasOfflineCamController {
	@Autowired
	private GasOfflineCamService gasOfflineCamService;

	@Autowired
	private DeptService deptService;
	@Autowired
	private GasStationinfoService gasStationinfoService;
	@Autowired
	private HttpSessionService httpSessionService;

	@ApiOperation(value = "新增")
	@PostMapping("gasOfflineCam/add")
	@RequiresPermissions("gasOfflineCam:add")
	@ResponseBody
	public DataResult add(@RequestBody GasOfflineCamEntity gasOfflineCam) {
		gasOfflineCamService.save(gasOfflineCam);
		return DataResult.success();
	}

	@ApiOperation(value = "删除")
	@DeleteMapping("gasOfflineCam/delete")
	@RequiresPermissions("gasOfflineCam:delete")
	@ResponseBody
	public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
		gasOfflineCamService.removeByIds(ids);
		return DataResult.success();
	}

	@ApiOperation(value = "更新")
	@PutMapping("gasOfflineCam/update")
	@RequiresPermissions("gasOfflineCam:update")
	@ResponseBody
	public DataResult update(@RequestBody GasOfflineCamEntity gasOfflineCam) {
		gasOfflineCamService.updateById(gasOfflineCam);
		return DataResult.success();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list0")
	// @RequiresPermissions("gasOfflineCam:list")
	@ResponseBody
	public DataResult list0(@RequestBody GasOfflineCamEntity gasOfflineCam) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		String taxorgcode = gasOfflineCam.getTaxorgcode();
		
		String currentDeptId = httpSessionService.getCurrentDeptId();
		
		String name = gasOfflineCam.getStationName();
		List stationinfoId = new ArrayList<>();
		if (TsBqUtils.isNotNull(name)) {
			LambdaQueryWrapper<GasStationinfoEntity> queryStationinfo = Wrappers.lambdaQuery();
			queryStationinfo.like(GasStationinfoEntity::getName, name);
			queryStationinfo.eq(GasStationinfoEntity::getIsAnalyse, "Y");
			List<GasStationinfoEntity> list = gasStationinfoService.list(queryStationinfo);
			for (GasStationinfoEntity stationinfo : list) {
				String id = stationinfo.getId();
				stationinfoId.add(id);
			}
		}else{
			LambdaQueryWrapper<GasStationinfoEntity> queryStationinfo = Wrappers.lambdaQuery();
			queryStationinfo.eq(GasStationinfoEntity::getIsAnalyse, "Y");
			List<GasStationinfoEntity> list = gasStationinfoService.list(queryStationinfo);
			for (GasStationinfoEntity stationinfo : list) {
				String id = stationinfo.getId();
				stationinfoId.add(id);
			}
		}
		
		Page page = new Page(gasOfflineCam.getPage(), gasOfflineCam.getLimit());
		
		QueryWrapper<GasOfflineCamEntity> queryWrapper = new QueryWrapper<>();

		LambdaQueryWrapper<GasOfflineCamEntity> lambdaQueryWrapper = queryWrapper
				.select("count(1) AS value,station_name as stationName,taxorg_name as taxorgName").lambda()
				.groupBy(GasOfflineCamEntity::getStationName)
				.groupBy(GasOfflineCamEntity::getTaxorgName);
		
		if (TsBqUtils.isNotNull(stationinfoId)) {
			lambdaQueryWrapper.in(GasOfflineCamEntity::getStationName, stationinfoId);
		}
		
		if(TsBqUtils.isNotNull(taxorgcode)){
			lambdaQueryWrapper.eq(GasOfflineCamEntity::getTaxorgName, taxorgcode);
			
		}else{
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				lambdaQueryWrapper.eq(GasOfflineCamEntity::getTaxorgName, currentDeptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局 
				lambdaQueryWrapper.like(GasOfflineCamEntity::getTaxorgName, currentDeptId.substring(0, 5));
			}else{
				// 省局
				
			}
			
		}
		
		
		if (!StringUtils.isEmpty(gasOfflineCam.getCreateTimeStart())) {
			lambdaQueryWrapper.ge(GasOfflineCamEntity::getOfflineTime, sdf.parse(gasOfflineCam.getCreateTimeStart() + " 00:00:00"));
		}
		if (!StringUtils.isEmpty(gasOfflineCam.getCreateTimeEnd())) {
			lambdaQueryWrapper.le(GasOfflineCamEntity::getOfflineTime, sdf.parse(gasOfflineCam.getCreateTimeEnd() + " 23:59:59"));
		}
		
		
		
		IPage<GasOfflineCamEntity> iPage = gasOfflineCamService.page(page, lambdaQueryWrapper);
		
		
		List<GasOfflineCamEntity> records = iPage.getRecords();

		for (GasOfflineCamEntity gasOfflineCamEntity : records) {
			String taxorgName = gasOfflineCamEntity.getTaxorgName();
			SysDept sysDept = deptService.getById(taxorgName);
			if (TsBqUtils.isNotNull(sysDept)) {
				gasOfflineCamEntity.setTaxorgName(sysDept.getName());
			}

			String stationName = gasOfflineCamEntity.getStationName();
			GasStationinfoEntity stationinfo = gasStationinfoService.getById(stationName);
			if (TsBqUtils.isNotNull(stationinfo)) {
				gasOfflineCamEntity.setStationName(stationinfo.getName());
			}

		}
		
		return DataResult.success(iPage);

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list")
	// @RequiresPermissions("gas:OfflineCam:list")
	@ResponseBody
	public DataResult findListByPage(@RequestBody GasOfflineCamEntity gasOfflineCam) throws ParseException {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String currentDeptId = httpSessionService.getCurrentDeptId();

		String name = gasOfflineCam.getStationName();
		
		List<String> stationinfoId = new ArrayList<>();
		if(TsBqUtils.isNotNull(gasOfflineCam.getStationId())){
			
			LambdaQueryWrapper<GasStationinfoEntity> queryStationinfo = Wrappers.lambdaQuery();
			queryStationinfo.eq(GasStationinfoEntity::getName, gasOfflineCam.getStationId());
			List<GasStationinfoEntity> list = gasStationinfoService.list(queryStationinfo);
			for (GasStationinfoEntity stationinfo : list) {
				String id = stationinfo.getId();
				stationinfoId.add(id);
			}
		}else{
			if (TsBqUtils.isNotNull(name)) {
				LambdaQueryWrapper<GasStationinfoEntity> queryStationinfo = Wrappers.lambdaQuery();
				queryStationinfo.like(GasStationinfoEntity::getName, name);
				List<GasStationinfoEntity> list = gasStationinfoService.list(queryStationinfo);
				for (GasStationinfoEntity stationinfo : list) {
					String id = stationinfo.getId();
					stationinfoId.add(id);
				}
			}
		}

		Page page = new Page(gasOfflineCam.getPage(), gasOfflineCam.getLimit());
		LambdaQueryWrapper<GasOfflineCamEntity> queryWrapper = Wrappers.lambdaQuery();
		
		if (TsBqUtils.isNotNull(stationinfoId)) {
			queryWrapper.in(GasOfflineCamEntity::getStationName, stationinfoId);
		}
		
		if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
			// 县区局
			queryWrapper.eq(GasOfflineCamEntity::getTaxorgName, currentDeptId);
		} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
			// 市局
			queryWrapper.like(GasOfflineCamEntity::getTaxorgName, currentDeptId.substring(0, 5));
		}else{
			// 省局
		}

		// 查询条件示例
		// queryWrapper.eq(GasOfflineCamEntity::getId, gasOfflineCam.getId());

		if (TsBqUtils.isNotNull(gasOfflineCam.getCamId())) {
			queryWrapper.eq(GasOfflineCamEntity::getCamId, gasOfflineCam.getCamId());
		}
		
		if (!StringUtils.isEmpty(gasOfflineCam.getCreateTimeStart())) {
			queryWrapper.ge(GasOfflineCamEntity::getOfflineTime, sdf.parse(gasOfflineCam.getCreateTimeStart() + " 00:00:00"));
		}
		if (!StringUtils.isEmpty(gasOfflineCam.getCreateTimeEnd())) {
			queryWrapper.le(GasOfflineCamEntity::getOfflineTime, sdf.parse(gasOfflineCam.getCreateTimeEnd() + " 23:59:59"));
		}
		
		IPage<GasOfflineCamEntity> iPage = gasOfflineCamService.page(page, queryWrapper);
		List<GasOfflineCamEntity> records = iPage.getRecords();

		for (GasOfflineCamEntity gasOfflineCamEntity : records) {
			String taxorgName = gasOfflineCamEntity.getTaxorgName();
			SysDept sysDept = deptService.getById(taxorgName);
			if (TsBqUtils.isNotNull(sysDept)) {
				gasOfflineCamEntity.setTaxorgName(sysDept.getName());
			}

			String stationName = gasOfflineCamEntity.getStationName();
			GasStationinfoEntity stationinfo = gasStationinfoService.getById(stationName);
			if (TsBqUtils.isNotNull(stationinfo)) {
				gasOfflineCamEntity.setStationName(stationinfo.getName());
			}

		}

		return DataResult.success(iPage);
	}

}
